trigger AccountTrigger on Account (after insert, after update) {
 
  /*
  *  This trigger is to circumvent a cross-object
  *  formula limitation introduced by
  *  Person Accounts.  This populates a lookup
  *  to the mirrored Contact record for a
  *  Person Account so that the fields are all
  *  accessible with a cross-object formula
  */
  Map<String, String> accIds = new Map<String, String>();
  List<Opportunity> lst_Opportunity = new List<Opportunity>();
  
  for(Account a : Trigger.new) { 
     
    if(a.Person_Account_Contact_ID__c == null && a.PersonContactId != null) {
     
      accIds.put(a.Id,a.PersonContactId);
    } 
    
  }
  lst_Opportunity = [ Select Id, StageName, Student_ID__c ,accountId from Opportunity where accountid IN : Trigger.NewMap.Keyset() and (stageName != 'Exited' or stageName != 'cambio de ciclo') ];
  List<Opportunity> updOpps = new List<Opportunity>();
  for(Opportunity opp : lst_Opportunity)
  {
      if(Trigger.newMap.get(opp.accountId).Student_Id__c != Trigger.oldMap.get(opp.accountId).Student_Id__c) {
          
          opp.Student_Id__C = Trigger.newMap.get(opp.accountId).Student_Id__c;  
          updOpps.add(opp);
      }    
 
  }  
  if(updOpps.size()>0)
  {
      update updOpps;
  }
 
  List<Account> accs = new  List<Account>() ;
  for(string aid : accIds.keySet()) {
    account acct = new account(id = aid, Person_Account_Contact_ID__c = accIds.get(aid));
    accs.add(acct); 
  }
 
  if(!accs.isEmpty()) {
    update accs;
  }
 
}